package utils
{
	import utils.StringTool;
	//计算器计算,不含UI
	public class CalculatorHandler
	{
		private static var firstNum:String="0";
		private static var secondNum:String="";
		private static var zhizhen:uint=0;//指针 用来标识目前是第一个数字还是第二个数字
		private static var operate:String="";//+-x/
		private static var _max_str_num:uint=11;//最大可以显示的字数,不包含小数点
		public function CalculatorHandler()
		{
		}
		
		public static function get max_str_num():uint
		{
			return _max_str_num;
		}
		
		public static function set max_str_num(value:uint):void
		{
			_max_str_num = value;
		}
		
		public static function clear():void
		{
			firstNum="0";
			secondNum="";
			zhizhen=0;
			operate="";
		}
		
		//"7","8","9","/","4","5","6","x","1","2","3","-","0",".","=","+","c"
		public static function getStr(str:String):String
		{
			//得到计算器按键
			if(str=="c")
			{
				clear();
				return "0";				
			}
			if(zhizhen==0)
			{
				if(str==".")
				{
					if(getDotIndex(firstNum)==-1)
					{
						//如果没有过小数点,是可以加入的
						firstNum+=".";
					}
					else
					{
						//如果有过,就不加了
					}
					return firstNum;
				}
				else if(StringTool.isAllNumber(str))
				{
					//如果是数字
					if(firstNum=="0")
					{
						firstNum=str;
					}
					else
					{
						firstNum+=str;
					}
					
					return firstNum;
				}
				else if(str=="=")
				{
					//nothing
					return firstNum;
				}
				else
				{
					//not 数字 not = not . 那肯定就是计算符号了
					if(str=="-")
					{
						if(firstNum=="0")
						{
							//如果是要赋值为负数
							firstNum="-"+firstNum;
							return firstNum;
						}
					}
					zhizhen=1;
					operate=str;
					return firstNum;
				}
			}
			else
			{
				if(str==".")
				{
					if(getDotIndex(secondNum)==-1)
					{
						//如果没有过小数点,是可以加入的
						secondNum+=".";
					}
					else
					{
						//如果有过,就不加了
					}
					return secondNum;
				}
				else if(StringTool.isAllNumber(str))
				{
					//如果是数字
					
					if(secondNum=="0")
					{
						secondNum=str;
					}
					else
					{
						secondNum+=str;
					}
					return secondNum;
				}
				else if(str=="=")
				{
					if(secondNum=="")
					{
						secondNum=firstNum;
						return calculation();
					}
					return calculation(true);
				}
				else
				{
					//not 数字 not = not . 那肯定就是计算符号了
					if(str=="-")
					{
						if(secondNum=="0")
						{
							//如果是要赋值为负数
							secondNum="-"+secondNum;
							return secondNum;
						}
					}
					if(zhizhen==0)
					{
						zhizhen=1;
						operate=str;
						return calculation();
					}
					else
					{
						return saveAftercalculation(str);
					}
				}
			}
			return firstNum;
		}
		
		private static function saveAftercalculation(_op:String):String
		{
			var num_0:Number=Number(firstNum);
			var num_1:Number=Number(secondNum);
			var result:Number;
			switch(operate)
			{
				case "+":
					result=num_0+num_1;
					break;
				case "-":
					result=num_0-num_1;
					break;
				case "x":
					result=num_0*num_1;
					break;
				case "/":
					result=num_0/num_1;
					break;
			}
			firstNum=result.toString();//计算后,firstNum自动=结果
			secondNum="";
			operate=_op;
			
			return formatNumber(result);
		}
		
		private static function calculation(_clear:Boolean=false):String
		{
			var num_0:Number=Number(firstNum);
			var num_1:Number=Number(secondNum);
			var result:Number;
			switch(operate)
			{
				case "+":
					result=num_0+num_1;
					break;
				case "-":
					result=num_0-num_1;
					break;
				case "x":
					result=num_0*num_1;
					break;
				case "/":
					result=num_0/num_1;
					break;
			}
			
			firstNum=result.toString();//计算后,firstNum自动=结果
			secondNum="";
			if(_clear)
			{
				zhizhen=0;
			}
			
			return formatNumber(result);
		}
		
		private static function formatNumber(num:Number):String
		{
			var str:String=num.toString();
			var dotIndex:int=getDotIndex(str);
			var strLength:uint=str.length;
			var dotLenght:uint=0;//小数点后位数
			if(dotIndex==-1)
			{
				//如果没有小数点
				if(strLength>_max_str_num)
				{
					return "error"+str.substr(0,5);
				}
				else
				{
					return str;
				}
			}
			else
			{
				return str.substr(0,_max_str_num+1);
			}
		}
		
		private static function getDotIndex(str:String):int
		{
			return str.indexOf(".");
		}
	}
}